package com.gem.talkbase.activity;



import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import com.gem.talkbase.R;
import com.gem.talkbase.fragment.FragmentIndicator;
import com.gem.talkbase.fragment.FragmentIndicator.OnIndicateListener;
import com.gem.talkbase.fragment.Fragment_Settings;
import com.gem.talkbase.fragment.Fragment_contact;
import com.gem.talkbase.fragment.Fragment_message;

/**
 * 功能描述：主Activity类，继承自FragmentActivity
 * 
 */

public class MainActivity extends FragmentActivity {

	public Fragment_contact fragment_contact;
	public static Fragment[] mFragments;
	public FragmentManager manager;
	public FragmentTransaction transaction;
	private View context;
	private LayoutInflater inflater;
	public String username;
	boolean isExit;  

   public void fun(){
	 manager=getSupportFragmentManager();
	 transaction=manager.beginTransaction();
	 Fragment_contact fragment_contact=new Fragment_contact();
	
	 Bundle bundle=new Bundle();
	 bundle.putString("use", username);//绑定登录id号
	 fragment_contact.setArguments(bundle);
	 transaction.add(R.id.fragment_search, fragment_contact);
	 transaction.commit();
    }
   public void fun1(String unick){
		 manager=getSupportFragmentManager();
		 transaction=manager.beginTransaction();
		Fragment_Settings fragment_Settings=new Fragment_Settings();
		
		 Bundle bundle=new Bundle();
		 bundle.putString("unick", unick);//绑定登录id号
		 fragment_Settings.setArguments(bundle);
		 transaction.add(R.id.fragment_settings_1, fragment_Settings);
		 transaction.commit();
	    }
  
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		Intent intent = getIntent();
	    username = intent.getStringExtra("username");//登录的id号
		System.out.println(username + "====MainActivity1---++++++++---------");
		if(username==null){
			Intent intent2 = getIntent();
		    username = intent2.getStringExtra("user_id");//登录的id号
		}
		
		 fun();
	     setFragmentIndicator(0);
	    Intent intent2=getIntent();
	    String unick=intent2.getStringExtra("user_unick");
	    if (unick!=null) {
			fun1(unick);
		}
   
	}   
	//==============================按home键或back到桌面，再次点图标，不会失去再次登录====================
	@Override
	public void onBackPressed() {  
	    //实现Home键效果  
	    //super.onBackPressed();这句话一定要注掉,不然又去调用默认的back处理方式了  
	    Intent i= new Intent(Intent.ACTION_MAIN);  
	    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
	    i.addCategory(Intent.CATEGORY_HOME);  
	    startActivity(i);   
	}
//======================按两次退出程序======================================
//	@Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            exit();
//            return false;
//        } else {
//            return super.onKeyDown(keyCode, event);
//        }
//    }
//	public void exit(){
//        if (!isExit) {
//            isExit = true;
//            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
//            mHandler.sendEmptyMessageDelayed(0, 2000);
//        } else {
//            Intent intent = new Intent(Intent.ACTION_MAIN);
//            intent.addCategory(Intent.CATEGORY_HOME);
//            startActivity(intent);
//            System.exit(0);
//        }
//    }
//	Handler mHandler = new Handler() {
//
//        @Override
//        public void handleMessage(Message msg) {
//            // TODO Auto-generated method stub
//            super.handleMessage(msg);
//            isExit = false;
//        }
//
//    };
	/**
	 * 初始化fragment
	 */
	private void setFragmentIndicator(int whichIsDefault) {
		mFragments = new Fragment[4];
		mFragments[0] = getSupportFragmentManager().findFragmentById(
				R.id.fragment_home);
		mFragments[1] = getSupportFragmentManager().findFragmentById(
				R.id.fragment_search);
		mFragments[2] = getSupportFragmentManager().findFragmentById(
				R.id.fragment_settings);
		mFragments[3] = getSupportFragmentManager().findFragmentById(
				R.id.fragment_settings_1);
		getSupportFragmentManager().beginTransaction().hide(mFragments[0])
				.hide(mFragments[1]).hide(mFragments[2]).hide(mFragments[3])
				.show(mFragments[whichIsDefault]).commit();

		FragmentIndicator mIndicator = (FragmentIndicator) findViewById(R.id.indicator);
		FragmentIndicator.setIndicator(whichIsDefault);
		mIndicator.setOnIndicateListener(new OnIndicateListener() {
			
			@Override
			public void onIndicate(View v, int which) {
				
				getSupportFragmentManager().beginTransaction()
						.hide(mFragments[0]).hide(mFragments[1])
						.hide(mFragments[2]).hide(mFragments[3])
						.show(mFragments[which]).commit();
			
			}
		});
		
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

}
